﻿using System.Web.Mvc;
using BaseControllerActionInvoker = System.Web.Mvc.ControllerActionInvoker;

namespace cludg.Web.Core.ControllerFactory
{
	public class ControllerActionInvoker : BaseControllerActionInvoker
	{
		protected readonly Filters.FilterRegistry FilterRegistry;

		public ControllerActionInvoker(Filters.FilterRegistry filterRegistry)
		{
			FilterRegistry = filterRegistry;
		}

		protected override FilterInfo GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
		{
			var filters = base.GetFilters(controllerContext, actionDescriptor);

			foreach (var filter in FilterRegistry.GetAuthorizationFilters())
				filters.AuthorizationFilters.Add(filter);

			foreach (var filter in FilterRegistry.GetActionFilters())
				filters.ActionFilters.Add(filter);

			foreach (var filter in FilterRegistry.GetResultFilters())
				filters.ResultFilters.Add(filter);

			foreach (var filter in FilterRegistry.GetExceptionFilters())
				filters.ExceptionFilters.Add(filter);

			return filters;
		}
	}
}
